package com.sydsen.imager;

import com.sydsen.imager.elements.Image;
import com.sydsen.imager.elements.Shape;
import com.sydsen.imager.elements.Text;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.InputStream;

/**
 * @Auther: syh
 * @Date: 2021/11/1
 * @Description:
 */
public class Images {

    private ImageRender render;

    private Images(ImageRender render) {
        this.render = render;
    }

    public static Images of(String filePath) throws Exception {
        ImageRender render = new ImageRender(filePath);
        return new Images(render);
    }

    public static Images of(BufferedImage image) throws Exception {
        ImageRender render = new ImageRender(image);
        return new Images(render);
    }

    public static Images of(InputStream stream) throws Exception {
        ImageRender render = new ImageRender(stream);
        return new Images(render);
    }

    public Image background() {
        Image image = this.render.getBackground();
        image.configure(this);
        return image;
    }

    public Text text(String txt) {
        Text text = new Text(txt);
        text.configure(this);
        return this.render.add(text);
    }

    public Image image(String path) throws Exception {
        Image image = new Image(path);
        image.configure(this);
        return this.render.add(image);
    }

    public Image image(BufferedImage img) {
        Image image = new Image(img);
        image.configure(this);
        return this.render.add(image);
    }

    public Image image(InputStream stream) throws Exception {
        Image image = new Image(ImageIO.read(stream));
        image.configure(this);
        return this.render.add(image);
    }

    public Shape shape(int height, Color color) {
        return shape(height, height, color);
    }

    public Shape shape(int height, int width, Color color) {
        Shape shape = new Shape(height, width, color);
        shape.configure(this);
        return this.render.add(shape);
    }

    public Images picType(ImageOptions.PicType picType) {
        this.render.setPicType(picType);
        return this;
    }

    public BufferedImage asBuffered() throws Exception {
        this.render.draw();
        return this.render.getBufferedImage();
    }

    public byte[] asBytes() throws Exception {
        this.render.draw();
        return this.render.getImageBytes();
    }

    public InputStream asStream() throws Exception {
        this.render.draw();
        return this.render.getImageStream();
    }

    public String asString() throws Exception {
        this.render.draw();
        return this.render.getImageString();
    }

    public void asFile(String path) throws Exception {
        this.render.draw();
        this.render.save(path);
    }
}
